// CaselessHashtable.java - A Hashtable that ignores the case of String keys.
//
// Copyright (C) 1999-2002  Smart Software Consulting
//
// This program is free software; you can redistribute it and/or
// modify it under the terms of the GNU General Public License
// as published by the Free Software Foundation; either version 2
// of the License, or (at your option) any later version.
//
// This program is distributed in the hope that it will be useful,
// but WITHOUT ANY WARRANTY; without even the implied warranty of
// MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
// GNU General Public License for more details.
//
// You should have received a copy of the GNU General Public License
// along with this program; if not, write to the Free Software
// Foundation, Inc., 59 Temple Place - Suite 330, Boston, MA  02111-1307, USA.
//
// Smart Software Consulting
// 1688 Silverwood Court
// Danville, CA  94526-3079
// USA
//
// http://www.smartsc.com
//

package com.smartsc.util;

import java.io.Serializable;
import java.util.Enumeration;
import java.util.Hashtable;
import java.util.Vector;

public class
CaselessHashtable
extends Hashtable
implements Cloneable, Serializable
{
	public CaselessHashtable()
	{
	}

	public CaselessHashtable( int initCap)
	{
		super( initCap);
	}

	public CaselessHashtable( int initCap, float loadFactor)
	{
		super( initCap, loadFactor);
	}

	private Hashtable keys = new Hashtable();

	public synchronized void clear()
	{
		super.clear();
		keys.clear();
	}
	public synchronized Object clone()
	{
		CaselessHashtable ch = (CaselessHashtable)super.clone();
		ch.keys = (Hashtable)keys.clone();
		return ch;
	}
	public synchronized boolean containsKey( Object key)
	{
		if( key instanceof String)
			key = ((String)key).toLowerCase();
		return super.containsKey( key);
	}
	public synchronized Object get( Object key)
	{
		if( key instanceof String)
			key = ((String)key).toLowerCase();
		Object o = super.get( key);
		if( o == null) return null;

		String s = null;
		if( o instanceof Vector) s = (String)((Vector)o).elementAt( 0);
		else if( o instanceof String) s = (String)o;
		else s = o.toString();

		return s;
	}
	public synchronized Enumeration getValues( Object key)
	{
		if( key instanceof String)
			key = ((String)key).toLowerCase();
		Vector values = (Vector)super.get( key);
		if( values == null) return null;
		return values.elements();
	}
	public synchronized Enumeration keys()
	{
		return keys.elements();
	}
	public synchronized Object put( Object key, Object value)
	{
		if( key instanceof String)
		{
			String lcKey = ((String)key).toLowerCase();
			keys.put( lcKey, key);
			key = lcKey;
		}
		else
		{
			keys.put( key, key);
		}
		Vector values = (Vector)super.get( key);
		if( values == null)
		{
			values = new Vector();
			super.put( key, values);
		}
		values.addElement( value);
		return null;
	}
	public synchronized Object remove( Object key)
	{
		if( key instanceof String)
		{
			key = ((String)key).toLowerCase();
		}
		keys.remove( key);
		super.remove( key);
		return null;
	}
}